package com.ccl.iot.publics;import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class StringFactory {
	private static String KCHARSET = (Charset.isSupported("GB18030") ? "GB18030" : Charset.defaultCharset().displayName());

	/**
	 * Paste the string value into the byte array
	 * @param aDst
	 * @param aPos
	 * @param aMax
	 * @param aString
	 */
	public static void PasteString(byte aDst[], int aPos, int aMax, String aString){
		if (aDst != null && aString != null){
			byte tBytes[] = aString.getBytes();
			
			if (tBytes.length < aMax){
				aMax = tBytes.length;
			}

			System.arraycopy(tBytes, 0, aDst, aPos, aMax);
		}
	}
	
	/**
	 * Resolve the string from the byte array
	 * @param aBytes
	 * @param aPos
	 * @param aLen
	 * @return
	 */
	public static String ResolveString(byte aBytes[], int aPos, int aLen){
		if (aBytes != null && aBytes.length >= aPos + aLen){
			try {
				return  (new String(aBytes, aPos, aLen, KCHARSET)).trim();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static String GetHashCode(Object ... aObject){
		if (aObject != null && aObject.length > 0){
			StringBuilder tHash = new StringBuilder();
			
			for (int i = 0; i < aObject.length; i++){
				if (i > 0){
					tHash.append('&');
				}
				
				tHash.append(i);
				tHash.append('@');
				
				if (aObject[i] != null){
					tHash.append(aObject[i].hashCode());
				}else{
					tHash.append("null");
				}
			}
			
			return tHash.toString();
		}
		
		return "";
	}
	
	public static String GetMixString(Object ... aObject){
		if (aObject != null && aObject.length > 0){
			StringBuilder tString = new StringBuilder();
			
			for (int i = 0; i < aObject.length; i++){
				if (i > 0){
					tString.append('&');
				}
				
				tString.append(i);
				tString.append('@');
				
				if (aObject[i] != null){
					tString.append(aObject[i].toString());
				}else{
					tString.append("null");
				}
			}
			
			return tString.toString();
		}
		
		return "";
	}

	/*
	 * Split an string by an delimiter
	 */
	public static String[] Split(String aString, String aDelimiter){
		String tmpSS[] = null;
		if ((aString != null) && (aDelimiter != null)){
			ArrayList<String> tmpStrList = new ArrayList<String>();
			
			int tmpDelimiterLen = aDelimiter.length();
			
			int tmpOPos = 0;
			int tmpNPos = 0;
			while ((tmpNPos = (aString.indexOf(aDelimiter, tmpOPos))) >= 0){
				tmpStrList.add(aString.substring(tmpOPos, tmpNPos));
				tmpOPos = tmpNPos + tmpDelimiterLen;
			}
			tmpStrList.add(aString.substring(tmpOPos, aString.length()));
			
			int tmpSize = tmpStrList.size();
			if (tmpSize > 0){
				tmpSS = new String[tmpSize];
				for (int j = 0 ; j < tmpSize ; j++){
					tmpSS[j] = tmpStrList.get(j);
				}
			}
		}
		return tmpSS;
	}
	
	/*
	 * Convert Hex string to byte array
	 */
	public static byte[] HexString2ByteArray(String aSrcHex, String aDelimiter){
		if (aSrcHex != null){
			int tmpSrcLen = aSrcHex.length();
			if (tmpSrcLen > 0){
				String tmpHexList[] = null;
				
				if (aDelimiter != null && aDelimiter.length() > 0){
					tmpHexList = Split(aSrcHex, aDelimiter);
				}else{
					if ((tmpSrcLen % 2) == 0){
						char tmpHexChar[] = aSrcHex.toCharArray();
						
						tmpSrcLen /= 2;
						
						tmpHexList = new String[tmpSrcLen];
						
						for (int i = 0 ; i < tmpSrcLen ; i++){
							tmpHexList[i] = String.copyValueOf(tmpHexChar, i * 2, 2);
						}
					}
				}
				
				if (tmpHexList != null){
					tmpSrcLen = tmpHexList.length;
					if (tmpSrcLen > 0){
						byte tBytes[] = new byte[tmpSrcLen];
						
						for (int i = 0 ; i < tmpSrcLen ; i++){
							tBytes[i] = Integer.valueOf(tmpHexList[i], 16).byteValue();
						}
						
						return tBytes;
					}
				}
			}		
		}
		
		return null;
	}
	
	/*
	 * Convert byte array to hex string
	 */
	public static String ByteArray2HexString(byte aSrcByteArray[], int aPos, int aLength, String aDelimiter){
		if (aSrcByteArray != null && aSrcByteArray.length >= aPos + aLength){
			java.lang.StringBuilder tmpBuilder = new java.lang.StringBuilder();
			boolean tmpHasDelimiter = false;
			
			if (aDelimiter != null){
				tmpHasDelimiter = aDelimiter.length() > 0;
			}
			
			aLength += aPos;
			for (int i = aPos ; i < aLength ; i++){
				tmpBuilder.append(Byte2Hex(aSrcByteArray[i]));
				
				if ((i < aLength - 1) && (tmpHasDelimiter)){
					tmpBuilder.append(aDelimiter);
				}
			}
			
			return tmpBuilder.toString();
		}
		
		return null;
	}
	
	/*
	 * Convert Hex to byte
	 */
	public static byte Hex2Byte(String aHex){
		return (byte) Integer.parseInt(aHex, 16);
	}
	
	/*
	 * Convert byte to Hex
	 */
	public static final String Byte2Hex(byte aByte){
		char tmpHex[] = Integer.toHexString(aByte).toCharArray();
		
		if (tmpHex.length >= 2){
			return String.copyValueOf(tmpHex, tmpHex.length - 2, 2);
		}else if (tmpHex.length == 1){
			return "0" + String.copyValueOf(tmpHex);
		}else{
			return String.copyValueOf(tmpHex);			
		}
	}
	
	/*
	 * Replace the match string with list
	 */
	public static String ReplaceEx(String aSourceText, String...aMatchAndReplaceList){
		int tmpArraySize = aMatchAndReplaceList.length;
		
		if ((tmpArraySize % 2) == 0){
			for (int i = 0 ; i < tmpArraySize ; i += 2){
				aSourceText.replaceAll(aMatchAndReplaceList[i], aMatchAndReplaceList[i + 1]);
			}
		}
		
		return aSourceText;
	}
	
	/*
	 * Joint string array with delimiter
	 */
	public static String Join(String aDelimiter, String...aStringArray){
		java.lang.StringBuilder tmpString = new java.lang.StringBuilder();
		
		if ((aDelimiter != null) && (aStringArray != null && aStringArray.length > 0)){
			if (aStringArray.length > 0){
				if (aStringArray[0] == null){
					tmpString.append("");					
				}else{
					tmpString.append(aStringArray[0]);
				}
			}
			for (int i = 1 ; i < aStringArray.length ; i++){
				if (aStringArray[i] == null){
					tmpString.append(aDelimiter).append("");					
				}else{
					tmpString.append(aDelimiter).append(aStringArray[i]);
				}
			}
		}
		
		return tmpString.toString();
	}
	
	public static String Join(String aDelimiter, ArrayList<String> aStringArray){
		java.lang.StringBuilder tmpString = new java.lang.StringBuilder();
		
		if ((aDelimiter != null) && (aStringArray != null)){
			if (aStringArray.size() > 0){
				if (aStringArray.get(0) == null){
					tmpString.append("");					
				}else{
					tmpString.append(aStringArray.get(0));
				}
			}
			
			for (int i = 0, tCount = aStringArray.size(); i < tCount ; i++){
				if (aStringArray.get(i) == null){
					tmpString.append(aDelimiter).append("");					
				}else{
					tmpString.append(aDelimiter).append(aStringArray.get(i));
				}
			}
		}
		
		return tmpString.toString();	
	}
	
	/*
	 * 
	 */
	public static String[] ParamArray2Array(String...aStringArray){
		String tString[] = null;
		
		if (aStringArray != null){
			if (aStringArray.length > 0){
				tString = new String[aStringArray.length];
				for (int i = 0; i < aStringArray.length; i++){
					tString[i] = aStringArray[i];
				}
			}
		}
		return tString;
	}
	
	/*
	 * Get string no null char
	 */
	public static String FullStr(String aSource){
		if (aSource.contains("\0")){
			if (aSource.startsWith("\0")){
				return "";
			}else{
				return aSource.substring(0, aSource.indexOf("\0") - 1);
			}
		}else{
			return aSource;
		}
	}
	
	public static String GetNoneNullString(String aString){
		if (aString != null){
			return aString;
		}else{
			return "";
		}
	}
	
	public static java.lang.StringBuilder GetNumericSequence(String aString){
		if (aString != null){
			java.lang.StringBuilder tSequence = new java.lang.StringBuilder();
			
			for (int i = 0, tSize = aString.length(); i < tSize; i++){
				char tChar = aString.charAt(i);
				
				if (tChar >= 48 && tChar <= 57){
					tSequence.append(tChar);
				}
			}
			
			if (tSequence.length() > 0){
				return tSequence;
			}
		}
		
		return null;
	}
	
	private static final String KSIZE_LEVEL_DESCRIPTION[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "NB", "DB"};
	public static String GetSizeDescription(long aSize, int aStartLevel){
		int tSizeLevel = aStartLevel;
		double tSize = aSize;
		DecimalFormat tFormat = new DecimalFormat("0.00");
		
		while (tSize > 1024){
			tSize /= 1024;
			tSizeLevel ++;
		}
		
		return tFormat.format(tSize) + KSIZE_LEVEL_DESCRIPTION[tSizeLevel];
	}
}
